package com.fxsh168.server.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.fxsh168.server.entitys.ModifyEntity;
import com.fxsh168.server.R;
import com.fxsh168.server.base.BaseActivity;
import com.fxsh168.server.interfaces.Constant;
import com.fxsh168.server.entitys.RegiYZMCG;
import com.fxsh168.server.utils.CountDownTimerUtils;
import com.fxsh168.server.utils.MD5Utils;
import com.fxsh168.server.utils.RegExp;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;


import java.util.List;

import okhttp3.Call;

public class ChangePhone extends BaseActivity {

    private EditText mPutNewPhone;
    private EditText mPutChangeYZM;
    private Button mQRModify;
    private String mGetNewPhone1;
    private String mYZM;
    private TextView mGetNewYZM;
    private String mYanzhengma;
    private String mPhone2;
    private String mPhone;
    private String mPassword;
    private String token;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_phone);
        getShare();
        mPutNewPhone = (EditText) findViewById(R.id.putNewPhone);
        mPutChangeYZM = (EditText) findViewById(R.id.putChangeYZM);
        mQRModify = (Button) findViewById(R.id.QRModify);
        mGetNewYZM = (TextView) findViewById(R.id.getNewYZM);
    }


    //获取验证码
    public void getNewYZM(View view) {
        mGetNewPhone1 = mPutNewPhone.getText().toString().trim();
        Log.e("getYanzhengma", "===" + mGetNewPhone1);
        if ((!TextUtils.isEmpty(mGetNewPhone1)) && mGetNewPhone1 != null ) {
            OkHttpUtils.post()
                    .url(Constant.CHANGEPHONEYZM)
                    .addParams("token", token)
                    .addParams("phone", mPhone)
                    .addParams("newphone", mGetNewPhone1)
                    .build()
                    .execute(new StringCallback() {
                        @Override
                        public void onError(Call call, Exception e, int id) {
                            Log.e("Tag", "===onError===" + e);
                            Toast.makeText(ChangePhone.this, "连接不上服务器!", Toast.LENGTH_SHORT).show();
                        }

                        @Override
                        public void onResponse(String response, int id) {

                            Log.e("Tag", "===onResponse===" + response + "===" + id);
                            if (response != null) {
                                RegiYZMCG regiYZMCG = new Gson().fromJson(response, RegiYZMCG.class);
//                                mYZM = ((String) regiYZMSB.getData()).trim();
                                Object data = regiYZMCG.getData();
                                if (data instanceof String) {
                                    String code = regiYZMCG.getCode().toString().trim();
                                    mYZM = regiYZMCG.getData().toString().trim();
//                                mYZM = mYZMCG.get(0);
                                    CountDownTimerUtils countDownTimerUtils = new CountDownTimerUtils(mGetNewYZM, 60000, 1000);
                                    countDownTimerUtils.start();
                                    if (code.equals(200 + "")) {
                                        Toast.makeText(ChangePhone.this, "恭喜您获取验证码成功!", Toast.LENGTH_SHORT).show();
                                    }
                                } else if (data instanceof List<?>) {
                                    String code = regiYZMCG.getCode().toString().trim();
                                    if (code.equals(201 + "")) {
                                        Toast.makeText(ChangePhone.this, "获取失败!", Toast.LENGTH_SHORT).show();
                                    } else if (code.equals(202 + "")) {
                                        Toast.makeText(ChangePhone.this, "非法请求!", Toast.LENGTH_SHORT).show();
                                    } else if (code.equals(204 + "")) {
                                        mPutNewPhone.setError("号码已存在!");
                                    }
                                }
                            }

                        }
                    });
        } else if ((TextUtils.isEmpty(mGetNewPhone1)) || mGetNewPhone1 == null) {
            mPutNewPhone.setError("手机号不能为空!");
        }  else if (mPhone.length() != 11) {
            mPutNewPhone.setError("请输入正确的手机号码!");
        }
    }

    //确认修改号码
    public void QRModify(View view) {
        mYanzhengma = mYZM;
        mPhone2 = mGetNewPhone1;
        //获取获得验证码之后输入框中的手机号
        String curPhone = mPutNewPhone.getText().toString().trim();
        Log.e("getYanzhengma", "===" + mPhone2 + "===" + mYZM + "===" + mYanzhengma);
        if (((!TextUtils.isEmpty(mPhone2)) && mPhone2 != null) && curPhone.equals(mPhone2)) {
            if ((mYZM.equals(mYanzhengma)) && (mYanzhengma != null && !TextUtils.isEmpty(mYanzhengma))) {
                OkHttpUtils.post()
                        .addParams("phone", mPhone)
                        .addParams("newphone", curPhone)
                        .url(Constant.QRMODIFY)
                        .build()
                        .execute(new StringCallback() {
                            @Override
                            public void onError(Call call, Exception e, int id) {
                                Log.e("Tag", "==onError==" + e);
                                Toast.makeText(ChangePhone.this, "连接不上服务器!", Toast.LENGTH_SHORT).show();
                            }

                            @Override
                            public void onResponse(String response, int id) {
                                if (response != null) {
                                    Log.e("Tag2", "==onResponse==" + response + "===" + id);
                                    RegiYZMCG regiYZMCG = new Gson().fromJson(response, RegiYZMCG.class);
                                    Object data = regiYZMCG.getData();
                                    String code = regiYZMCG.getCode().toString().trim();
                                    if (code.equals(200 + "")) {
                                        Log.e("200", "成功!");
                                        Toast.makeText(ChangePhone.this, "恭喜您，修改成功!", Toast.LENGTH_SHORT).show();
                                        SharedPreferences sp = getSharedPreferences(Constant.LOGINCLASS, MODE_PRIVATE);
                                        String mPhone = sp.getString("mPhone", "");
                                        String curPassword = sp.getString("mPassword", "");
                                        //密码加密
                                        String jMPassword = MD5Utils.MD532(curPassword);
                                        //token加密
                                        String curToken = MD5Utils.MD532("lis" + mGetNewPhone1 + "bin" + jMPassword + "www625kejicom");
                                        //传值到登录页面
                                        ModifyEntity modf = new ModifyEntity(mPhone2, curPassword, curToken);
//                                                EventBus.getDefault().post(modf);
                                        Intent intent = new Intent(ChangePhone.this, LoginActivity.class);
                                        startActivity(intent);
                                        ChangePhone.this.finish();
                                    } else if (code.equals(201 + "")) {
                                        showToast("非法请求!");
                                    } else if (code.equals(202 + "")) {
                                        showToast("修改失败!");
                                    }
                                }
                            }
                        });

            } else {
                mPutChangeYZM.setError("验证码不正确!");
            }
        } else {
            mPutNewPhone.setError("请输入11位正确的手机号!");
        }

    }

    //获取登录时候保存的值
    public void getShare() {
        SharedPreferences sp = getSharedPreferences(Constant.LOGINCLASS, MODE_PRIVATE);
        mPhone = sp.getString("mPhone", "");
        mPassword = sp.getString("mPassword", "");
        token = sp.getString("token", "");
    }

}
